package org.springboot.szsrcb.controller;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springboot.szsrcb.entity.Attachment;
import org.springboot.szsrcb.srvice.Att_Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/attachment")
public class AttachsRestController {

	private static final Logger logger = LoggerFactory.getLogger(AttachsRestController.class);

	@Autowired
	private Att_Service att_Service;

	@RequestMapping("/upload")
	public void att_upload(HttpServletRequest request, @RequestParam(value = "file") MultipartFile[] files,
			HttpServletResponse response) {

		logger.info("request.getMethod:" + request.getMethod());
		logger.info("access /attachment/upload");
		boolean isMultipartContent = ServletFileUpload.isMultipartContent(request);
		logger.info("ServletFileUpload.isMultipartContent : " + isMultipartContent);

		DiskFileItemFactory factory = new DiskFileItemFactory();
		factory.setSizeThreshold(4096);
		// factory.
		factory.setRepository(new File("d:/SZSRCB/tmp"));

		Map<String, Object> map = new HashMap<String, Object>();

		SimpleDateFormat sdf = new SimpleDateFormat("MMddHHmmss");
		String tmp_s_date = sdf.format(new java.util.Date());
		int v_relation_req_id = Integer.parseInt(tmp_s_date);

		map.put("relation_req_id", v_relation_req_id);

		for (MultipartFile mf : files) {
			if (!mf.isEmpty()) {
				// String
				String filePath = "D:/SZSRCB/tmp/" + mf.getOriginalFilename();
				logger.info("filePath : " + filePath);
				try {
					byte[] bytes = mf.getBytes();
					InputStream inputStream = null;
					inputStream = mf.getInputStream();
					map.put("MultipartFile", mf);
					map.put("InputStream", inputStream);
					Attachment r_att_id = att_Service.new_req_add_a_att(map);

					PrintWriter out = response.getWriter();
					// out.print("{state: true, name:'CCS_UAT-配置DNS.bat', extra:
					// {info: 'just a way to send some extra data', param: 'some
					// value here'}}");
					String s = "{state: true, name:'" + mf.getOriginalFilename() + "', extra: {att_gropid: '"
							+ v_relation_req_id + "', att_ids: '" + r_att_id.getId() + "'}}";
					out.print(s);
					logger.info("return info  : " + s);
				} catch (IllegalStateException | IOException e) {
					e.printStackTrace();
				}
			}
		}

	}
	@RequestMapping("/new_test_upload")
	public void new_test_upload(HttpServletRequest request, @RequestParam(value = "file") MultipartFile[] files,
			HttpServletResponse response) {

		logger.info("request.getMethod:" + request.getMethod());
		logger.info("access /attachment/upload_test_assign");
		boolean isMultipartContent = ServletFileUpload.isMultipartContent(request);
		logger.info("ServletFileUpload.isMultipartContent : " + isMultipartContent);

		DiskFileItemFactory factory = new DiskFileItemFactory();
		factory.setSizeThreshold(4096);
		// factory.
		factory.setRepository(new File("d:/SZSRCB/tmp"));

		Map<String, Object> map = new HashMap<String, Object>();

		SimpleDateFormat sdf = new SimpleDateFormat("MMddHHmmss");
		String tmp_s_date = sdf.format(new java.util.Date());		
		int v_relation_test_id = Integer.parseInt(tmp_s_date);

		map.put("relation_test_id", v_relation_test_id);

		for (MultipartFile mf : files) {
			if (!mf.isEmpty()) {
				// String
				String filePath = "D:/SZSRCB/tmp/" + mf.getOriginalFilename();
				logger.info("filePath : " + filePath);
				try {
					byte[] bytes = mf.getBytes();
					InputStream inputStream = null;
					inputStream = mf.getInputStream();
					map.put("MultipartFile", mf);
					map.put("InputStream", inputStream);
					Attachment r_att_id = att_Service.new_test_add_a_att(map);

					PrintWriter out = response.getWriter();
					// out.print("{state: true, name:'CCS_UAT-配置DNS.bat', extra:
					// {info: 'just a way to send some extra data', param: 'some
					// value here'}}");
					String s = "{state: true, name:'" + mf.getOriginalFilename() + "', extra: {att_gropid: '"
							+ v_relation_test_id + "', att_ids: '" + r_att_id.getId() + "'}}";
					out.print(s);
					logger.info("return info  : " + s);
				} catch (IllegalStateException | IOException e) {
					e.printStackTrace();
				}
			}
		}

	}
	
	@RequestMapping("/upload_test_assign")
	public void upload_test_assign(HttpServletRequest request, @RequestParam(value = "file") MultipartFile[] files,
			HttpServletResponse response) {

		logger.info("request.getMethod:" + request.getMethod());
		logger.info("access /attachment/upload_test_assign");
		boolean isMultipartContent = ServletFileUpload.isMultipartContent(request);
		logger.info("ServletFileUpload.isMultipartContent : " + isMultipartContent);

		DiskFileItemFactory factory = new DiskFileItemFactory();
		factory.setSizeThreshold(4096);
		// factory.
		factory.setRepository(new File("d:/SZSRCB/tmp"));

		Map<String, Object> map = new HashMap<String, Object>();

		SimpleDateFormat sdf = new SimpleDateFormat("MMddHHmmss");
		String tmp_s_date = sdf.format(new java.util.Date());		
		int v_relation_test_id = Integer.parseInt(tmp_s_date);

		map.put("relation_test_id", v_relation_test_id);

		for (MultipartFile mf : files) {
			if (!mf.isEmpty()) {
				// String
				String filePath = "D:/SZSRCB/tmp/" + mf.getOriginalFilename();
				logger.info("filePath : " + filePath);
				try {
					byte[] bytes = mf.getBytes();
					InputStream inputStream = null;
					inputStream = mf.getInputStream();
					map.put("MultipartFile", mf);
					map.put("InputStream", inputStream);
					Attachment r_att_id = att_Service.assign_test_add_a_att(map);

					PrintWriter out = response.getWriter();
					// out.print("{state: true, name:'CCS_UAT-配置DNS.bat', extra:
					// {info: 'just a way to send some extra data', param: 'some
					// value here'}}");
					String s = "{state: true, name:'" + mf.getOriginalFilename() + "', extra: {att_gropid: '"
							+ v_relation_test_id + "', att_ids: '" + r_att_id.getId() + "'}}";
					out.print(s);
					logger.info("return info  : " + s);
				} catch (IllegalStateException | IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	@RequestMapping("/upload_test_modify")
	public void upload_test_modify(HttpServletRequest request, @RequestParam(value = "file") MultipartFile[] files,
			HttpServletResponse response) {

		logger.info("request.getMethod:" + request.getMethod());
		logger.info("access /attachment/upload_test_modify");
		boolean isMultipartContent = ServletFileUpload.isMultipartContent(request);
		logger.info("ServletFileUpload.isMultipartContent : " + isMultipartContent);

		DiskFileItemFactory factory = new DiskFileItemFactory();
		factory.setSizeThreshold(4096);
		// factory.
		factory.setRepository(new File("d:/SZSRCB/tmp"));

		Map<String, Object> map = new HashMap<String, Object>();

		SimpleDateFormat sdf = new SimpleDateFormat("MMddHHmmss");
		String tmp_s_date = sdf.format(new java.util.Date());		
		int v_relation_test_id = Integer.parseInt(tmp_s_date);

		map.put("relation_test_id", v_relation_test_id);

		for (MultipartFile mf : files) {
			if (!mf.isEmpty()) {
				// String
				String filePath = "D:/SZSRCB/tmp/" + mf.getOriginalFilename();
				logger.info("filePath : " + filePath);
				try {
					byte[] bytes = mf.getBytes();
					InputStream inputStream = null;
					inputStream = mf.getInputStream();
					map.put("MultipartFile", mf);
					map.put("InputStream", inputStream);
					Attachment r_att_id = att_Service.modify_test_add_a_att(map);

					PrintWriter out = response.getWriter();
					// out.print("{state: true, name:'CCS_UAT-配置DNS.bat', extra:
					// {info: 'just a way to send some extra data', param: 'some
					// value here'}}");
					String s = "{state: true, name:'" + mf.getOriginalFilename() + "', extra: {att_gropid: '"
							+ v_relation_test_id + "', att_ids: '" + r_att_id.getId() + "'}}";
					out.print(s);
					logger.info("return info  : " + s);
				} catch (IllegalStateException | IOException e) {
					e.printStackTrace();
				}
			}
		}

	}
	
	@RequestMapping("/modify_req_att_upload")
	public void modify_req_att_upload(HttpServletRequest request, @RequestParam(value = "file") MultipartFile[] files,
			HttpServletResponse response) {

		logger.info("request.getMethod:" + request.getMethod());
		logger.info("access /attachment/upload");
		boolean isMultipartContent = ServletFileUpload.isMultipartContent(request);
		logger.info("ServletFileUpload.isMultipartContent : " + isMultipartContent);

		DiskFileItemFactory factory = new DiskFileItemFactory();
		factory.setSizeThreshold(4096);
		// factory.
		factory.setRepository(new File("d:/SZSRCB/tmp"));

		Map<String, Object> map = new HashMap<String, Object>();

		SimpleDateFormat sdf = new SimpleDateFormat("MMddHHmmss");
		String tmp_s_date = sdf.format(new java.util.Date());
		int v_relation_req_id = Integer.parseInt(tmp_s_date);

		map.put("relation_req_id", v_relation_req_id);

		for (MultipartFile mf : files) {
			if (!mf.isEmpty()) {
				// String
				String filePath = "D:/SZSRCB/tmp/" + mf.getOriginalFilename();
				logger.info("filePath : " + filePath);
				try {
					byte[] bytes = mf.getBytes();
					InputStream inputStream = null;
					inputStream = mf.getInputStream();
					map.put("MultipartFile", mf);
					map.put("InputStream", inputStream);
					Attachment r_att_id = att_Service.new_req_add_a_att(map);

					PrintWriter out = response.getWriter();
					// out.print("{state: true, name:'CCS_UAT-配置DNS.bat', extra:
					// {info: 'just a way to send some extra data', param: 'some
					// value here'}}");
					String s = "{state: true, name:'" + mf.getOriginalFilename() + "', extra: {att_gropid: '"
							+ v_relation_req_id + "', att_ids: '" + r_att_id.getId() + "'}}";
					out.print(s);
					logger.info("return info  : " + s);
				} catch (IllegalStateException | IOException e) {
					e.printStackTrace();
				}
			}
		}

	}

}
